package com.lanou.dllo.vc.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.easemob.util.ImageUtils;
import com.lanou.dllo.vc.R;
import com.lanou.dllo.vc.task.DownloadImageTask;
import com.lanou.dllo.vc.util.ImageCache;

import java.io.File;

import butterknife.Bind;
import butterknife.ButterKnife;

public class AddFriendDialog extends BaseActivity {

@Bind(R.id.title)
    TextView mTextView;

    @Bind(R.id.btn_cancel)
    Button mCancelBtn;

    @Bind(R.id.image)
    ImageView imageView;

    @Bind(R.id.edit)
    EditText editText;
    private  boolean isEditShow;
    private int position;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_add_friend_dialog;
    }

    @Override
    protected void initView() {
        //提示内容
        String msg = getIntent().getStringExtra("msg");
        //提示标题
        String title = getIntent().getStringExtra("title");
        position = getIntent().getIntExtra("position",-1);
        //是否显示取消标题
        boolean isCanceTitle = getIntent().getBooleanExtra("titleIsCancle",false);
        //是否显示取消按钮
        boolean isCanceShow = getIntent().getBooleanExtra("cancle",false);
        //是否显示文本编辑
        isEditShow = getIntent().getBooleanExtra("editTextShow",false);
        //转发复制的图片的path
        String path = getIntent().getStringExtra("farwardImage");
        //
        String edit_text = getIntent().getStringExtra("edit_text");
        if(msg != null)
            ((TextView)findViewById(R.id.alert_message)).setText(msg);
        if(title != null)
            mTextView.setText(title);
        if(isCanceTitle){
            mTextView.setVisibility(View.GONE);
        }
        if(isCanceShow)
            mCancelBtn.setVisibility(View.VISIBLE);
        if(path != null){
            //优先拿大图，没有去取缩略图
            if(!new File(path).exists())
                path = DownloadImageTask.getThumbnailImagePath(path);
            imageView.setVisibility(View.VISIBLE);
            ((TextView)findViewById(R.id.alert_message)).setVisibility(View.GONE);
            if(ImageCache.getInstance().get(path) != null){
                imageView.setImageBitmap(ImageCache.getInstance().get(path));
            }else{
                Bitmap bm = ImageUtils.decodeScaleImage(path, 150, 150);
                imageView.setImageBitmap(bm);
               ImageCache.getInstance().put(path, bm);
            }

        }
        if(isEditShow){
            editText.setVisibility(View.VISIBLE);
            editText.setText(edit_text);
        }
    }

    @Override
    protected void initData() {

    }


    public void ok(View view){
        setResult(RESULT_OK,new Intent().putExtra("position",position).
                putExtra("edittext",editText.getText().toString()));

        if (position == -1){
            //CharActivity.resendPos = postion;
            finish();
        }
    }

    public void cancel(View view){
        finish();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        finish();
        return  true;
    }
}
